package com.ygj.documentReceipt.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.base.common.persistent.dao.DictnameDAO;
import com.base.common.persistent.dao.NotesDAO;
import com.base.common.persistent.dao.SignDAO;
import com.gexin.rp.sdk.base.uitls.Base64Util;
import com.ygj.base.common.persistent.model.Notes;
import com.ygj.base.common.persistent.model.Sign;
import com.ygj.base.webservice.entity.announcement.Note;
import com.ygj.documentReceipt.service.DocumentReceiptService;

@Service("documentReceiptService")
public class DocumentReceiptServiceImpl implements DocumentReceiptService {

	@Resource
	private DictnameDAO dicnameDao;

	@Resource
	private SignDAO signDao;
	
	@Resource 
	private NotesDAO notesDao;

	@Override
	public int authentication(String username, String phone) {
		int szie = dicnameDao.authenticationExied(username, phone);
		// System.out.println(szie);
		return szie;
	}

	@Override
	public List<Sign> queryOfficialDocumentSign(String username) {
		//String sql = "select model.fno, model.fbm, model.ftitle, model.fdate from Sign as model where model.fmen = ? and model.fpclass = '签收' order by model.fdate desc";
		//List<Object> properties = new ArrayList<Object>();
		//properties.add(username);
		List<Sign> list = signDao.findByProperties(username, "签收");
//		List<Sign> signs = new ArrayList<Sign>();
		/*for (Object sign : list) {
			Object[] objs = (Object[]) sign;
			Sign sign2 = new Sign();
			sign2.setFno(String.valueOf(objs[0]));
			sign2.setFbm(String.valueOf(objs[1]));
			sign2.setFtitle(String.valueOf(objs[2]));
			sign2.setFdate(String.valueOf(objs[3]));
			signs.add(sign2);
		}*/

		return list;
	}

	@Override
	public int queryOfficialDocumentSignCounts(String username) {
		//String sql = "select model.fno from Sign as model where model.fmen = ? and model.fpclass = '签收'";
		/*List<Object> properties = new ArrayList<Object>();
		properties.add(username);*/
		return signDao.findByProperties(username, "签收").size();
	}

	@Override
	public Note queryOfficialDocumentSignDetail(String fno) {
		/*String sql = "from Sign as model where model." + SignDAO.FNO + " = ? and model." + SignDAO.FMEN + " = ?";
		List<Object> properties = new ArrayList<Object>();
		properties.add(fno);
		properties.add(username);
		List<Sign> list = signDao.findByProperties(sql, properties);*/
		
//		;
//		List<Sign> signs = new ArrayList<Sign>();
		
		Notes notes = notesDao.findByFno(fno);
		Note note = new Note();
		note.setFbm(notes.getFbm());
		note.setFclass(notes.getFclass());
		note.setFcom(notes.getFcom());
		note.setFcon(notes.getFcon());
		note.setFcurmen(notes.getFcurmen());
		note.setFdate(notes.getFdate());
		note.setFfb(notes.getFfb());
		note.setFmen1(notes.getFmen1());
		note.setFmen2(notes.getFmen2());
		note.setFmt(notes.getFmt());
		note.setFmj(notes.getFmj());
		note.setFno(notes.getFno());
		note.setFoc(notes.getFoc());
		note.setFps(notes.getFps());
		note.setFpub(notes.getFpub());
		note.setFpubtime(notes.getFpubtime());
		note.setFseed(notes.getFseed());
		note.setFsend(notes.getFsend());
		note.setFser(notes.getFser());
		note.setFsize(notes.getFsize());
		note.setFsp(notes.getFsp());
		note.setFstate(notes.getFstate());
		note.setFtitle(notes.getFtitle());
		note.setFupdate(notes.getFupdate());
		note.setRowguid(notes.getRowguid());
		note.setFstmStr(Base64Util.getBASE64(notes.getFstm()));
		return note;
	}

	@Transactional
	@Override
	public int sign(String username, String fno) {
		int result = 0;
		String sql = "update sign set fpclass='已收' where fpclass='签收' and fmen = '" + username + "' and fno = '" + fno + "'";
		/*List<Object> properties = new ArrayList<Object>();
		properties.add(username);
		properties.add(fno);*/
		result = signDao.updateSomeByProperties(sql);
		if (result > 0) {
			result = 1;
			return result;
		}
		return result;
	}

	@Transactional
	@Override
	public int signAll(String username) {
		int result = 0;
		String sql = "update sign set fpclass='已收' where fpclass='签收' and fmen = '" + username + "'";
		/*List<Object> properties = new ArrayList<Object>();
		properties.add(username);*/
		result = signDao.updateSomeByProperties(sql);
		if (result > 0) {
			result = 1;
			return result;
		}
		return result;
	}
}
